//调用标签类型浮层
var pageTapType = null;
$('#taptype_add').live('click', function(){
//	if($("#tapcode").val().length > 0){
//		$('#sortText').val('');
		pageTapType = $.layer({
			type: 1,
			title: false,
			area: ['575', 'auto'],
			border: [0],
			shade: [0.3,'#000'],
			closeBtn: [0, false],
			fadeIn: 500,
			move:".taptype_layer .tit",
			page:{dom:'.taptype_layer'}
		});
//	}else{
//		layer.msg("没有选择左边标签类别数据,无法新增数据！",2,-1);
//	}	

	//自设关闭
	$('.taptype_layer > .closeBtn,.taptype_layer .cancelLayer').live('click', function(){
		layer.close(pageTapType);
	});
	$('#tapTypeAddArea').validationEngine('validate');
});	

var pageTapTypeEdit = null;
$('.typeEdit').live('click', function(){
	pageTapTypeEdit = $.layer({
		type: 1,
		title: false,
		area: ['575', 'auto'],
		border: [0],
		shade: [0.3,'#000'],
		closeBtn: [0, false],
		fadeIn: 500,
		move:".taptype_layerEdit .tit",
		page:{dom:'.taptype_layerEdit'}
	});
	
	//自设关闭
	$('.taptype_layerEdit > .closeBtn,.taptype_layerEdit .cancelLayer').live('click', function(){
		layer.close(pageTapTypeEdit);
	});

	var tapTypeCode = $(this).attr("tapTypeCode");
	var tapTypeName = $(this).attr("tapTypeName");
	$("#tapTypeCode").val(tapTypeCode);
	$("#tapTypeEdit").val(tapTypeName);
	$('#tapTypeEditArea').validationEngine('validate');
});	



//调用标签值浮层
var pageTapValue = null;
$('#typevalue_add').live('click', function(){
	if($("#tapcode").val().length > 0){
		pageTapValue = $.layer({
			type: 1,
			title: false,
			area: ['575', 'auto'],
			border: [0],
			shade: [0.3,'#000'],
			closeBtn: [0, false],
			fadeIn: 500,
			move:".tapvalue_layer .tit",
			page:{dom:'.tapvalue_layer'}
		});
	}else{
		layer.msg("没有选择左边标签类别数据,无法新增数据！",2,-1);
	}	

	//自设关闭
	$('.tapvalue_layer > .closeBtn,.tapvalue_layer .cancelLayer').live('click', function(){
		layer.close(pageTapValue);
	});
	$('#tapValueAddArea').validationEngine('validate');
});	

var pageTapValueEdit = null;
$('.valueEdit').live('click', function(){
	pageTapValueEdit = $.layer({
		type: 1,
		title: false,
		area: ['575', 'auto'],
		border: [0],
		shade: [0.3,'#000'],
		closeBtn: [0, false],
		fadeIn: 500,
		move:".tapvalue_layerEdit .tit",
		page:{dom:'.tapvalue_layerEdit'}
	});
	
	//自设关闭
	$('.tapvalue_layerEdit > .closeBtn,.tapvalue_layerEdit .cancelLayer').live('click', function(){
		layer.close(pageTapValueEdit);
	});
	//这里是标签值修改时赋值
	var tapValueCode = $(this).attr("tapValueCode");
	var tag_id = $(this).attr("tag_id");
	var tagvalue_id = $(this).attr("tagvalue_id");
	$("#tapValueEdit").val(tapValueCode);
	//类别ID
	$("#tapValueCode").val(tag_id);
	$("#tapValueID").val(tagvalue_id);
	//savaData.loadTapValueAjax(tapValueCode);
	$('#tapValueEditArea').validationEngine('validate');
});	

var s ={
	//查询标签类别
	"tapTypeLoadData":function(data){
     	var objCount = data.returnData.length; //取得组织管理列表
	   	var htmlList = "";
	   	for(var i=0;i<objCount; i++){
	   		var tapTypeCode = data.returnData[i].tag_id;
	   		var tapTypeName = data.returnData[i].tag_type;
			htmlList = htmlList+'<tr class="td"><td>'+tapTypeName+'</td><td><a href="javascript:void(0);" class="typeEdit" tapTypeName='+tapTypeName+' tapTypeCode='+tapTypeCode+'>编辑</a><a href="javascript:void(0);" class="removeTypeBtn" tapTypeCode='+tapTypeCode+'>删除</a></td></tr>';
	    }
	    var strHead = "<tr class='th'><th width='100'>标签类别</th><th>操作</th></tr>";
	    $("#selectTapType").html(strHead + htmlList);	
	},	  		
	"tapTypePublicAjax":function(){
		var _obj = $("#selectTapType");
		$.ajax({
			type: "POST",
			url: basePath + "Tagtype/queryAllTag.do",
			error:function(request) {
				layer.msg("加载失败，请刷新重试！",2,-1);
		    },
		    success:function(data) {                    
                if (data.returnCode==10000) {
                     
                    s.tapTypeLoadData(data);
                    $('#selectTapType').find('.td').eq(0).trigger('click');
                                    
                } else if(data.returnCode==999994){
                    
                    setUserTimeout(data);
                    
                }},
	        beforeSend:function(){
	        	var imgGif = "<p class='loadingGif'></p>";
	        	_obj.html(imgGif);
	    	} 

		});	  			
	},
};

//页面初始化加载数据
s.tapTypePublicAjax();





//标签类别编码查询对应的标签值列表
var q ={
	//查询标签值
	"tapValueLoadData":function(data){
     	var objCount = data.returnData.length; 
	   	var htmlList = "";
	   	for(var i=0;i<objCount; i++){
	   		var tapValueCode = data.returnData[i].tag_value;
	   		var tag_id = data.returnData[i].tag_id;
	   		var tagvalue_id = data.returnData[i].tagvalue_id;
			htmlList = htmlList+'<tr class="td"><td>'+tapValueCode+'</td><td><a href="javascript:void(0);" class="valueEdit" tag_id='+tag_id+' tagvalue_id='+tagvalue_id+' tapValueCode='+tapValueCode+'>编辑</a><a href="javascript:void(0);" class="removeValueBtn" tagvalue_id='+tagvalue_id+' tapValueCode='+tapValueCode+'>删除</a></td></tr>';
	    }
	    var strHead = "<tr class='th'><th width='160'>标签值</th><th>操作</th></tr>";
	    var strPage ="<tr class='pages'><td colspan='5'><div class='ui_page' id='pageArea'></div></td></tr>"
	    if(objCount<1){
	    	$("#selectDicList").html(strHead);	
	    }else{
	    	$("#selectDicList").html(strHead + htmlList+ strPage);	
	    }
	},	  		
	//public load ajax
	"publicAjax":function(pIndex,numList,selectJson){	
		var _obj = $("#CustomsAreaList");
		var strtapcode="";
		strtapcode = $("#tapcode").val();
		$.ajax({
			//模拟接口数据
			type: "POST",
			url: basePath + "Tagvalue/queryByParams.do?pageIndex="+pIndex+"&pageSize="+numList+"&tag_id="+strtapcode,
			data:selectJson,
			error:function(request) {
				layer.msg("加载失败，请刷新重试！",2,-1);
		    },
		    success:function(data) {                    
                if (data.returnCode==10000) {
                    q.tapValueLoadData(data);
                    paging.loadPage(data); 
                } else if(data.returnCode==999994){
                    
                    setUserTimeout(data);
                    
                }if(data.returnData.length<=0){
                	_obj.html("<br><bR><p align='center'>很遗憾，没有查询到相关的数据！</p><br><bR>");
                }else if(data.returnData.length>0){_obj.html(" ");
                }
                }, 
	        beforeSend:function(){
	        	var imgGif = "<p class='loadingGif'></p>";
	        	$("#selectDicList").html(imgGif);
	    	} 

		});	  	
	},
}

$("#selectTapType .td").live("click",function(){
	var nameHtml = $(this).html();
//	var isOf = nameHtml.indexOf("地域");
//	if(isOf>-1){
//		$("#taptype_add").hide();
//	}else{
//		$("#taptype_add").show();
//	}
	$(this).addClass("hover").siblings().removeClass("hover");
	$("#tapcode").val($(this).children("td").eq(1).children("a").attr("tapTypeCode"));
//	$("#taptype").val($(this).children("td").eq(1).attr("taptype"));
//	$(".dictypeOutput").html($("#dictype").val());
	curPage=1;
	q.publicAjax(curPage,numList,"");
	
})

//添加
var savaData={
	//编辑获取标签类别
	"getTapTypeFormVal":function(data){
		var tapType =data.returnData.tapType;
		$("#tapTypeEdit").val(tapType);
	},
	//public loadAjax
//	"loadTapTypeAjax":function(tapTypeCode){
//		$.ajax({
////			type: "POST",
////	        url: basePath+"dictionary/selectByPrimaryKey",
//			//模拟接口数据
//			type: "GET",
//	        url: basePath+"json/dictionary/selectByPrimaryKey.json",
//	        data: {"tapTypeCode":tapTypeCode},
//	        success: function (data) {
//                if (data.returnCode==0) {
//    
//                    if (data.returnCode==0&&data.returnData!=null&& data.returnData.length!=0) {
//                        savaData.getTapTypeFormVal(data);
////                      $("#sortEext").attr('data-vi',data.returnData.maxSort)
//                    }else{
//                           layer.msg("加载失败，请重试！",2,-1);
//                    }
//                
//                } else if(data.returnCode==999994){
//
//                    setUserTimeout(data);
//
//                }},
//	        error: function (message) {
//	            layer.msg("加载失败，请重试！",2,-1);
//	        }
//	    });	
//	},
	//标签类别数据处理
	"addTapTypeFrom":function(){
		var tapTypeEdit = $("#tapTypeAdd").val().trim();
		var jsons = {
			"tag_type":tapTypeEdit
		};
		return jsons;
	},
	//新增标签类别
	"postTapTypeAjax":function(){
		$.ajax({
			type: 'POST',
			url: basePath + 'Tagtype/addTagtype.do',
			data: savaData.addTapTypeFrom(),
			success: function (data) {
                if (data.returnCode==10000) {
                    layer.msg(data.returnMessage,1,10);   
                    layer.close(pageTapType);
//                  curPage=1;
                    q.publicAjax(curPage,numList,"");     
//                  $("#tapTypeText,#sortText").val("");
					s.tapTypePublicAjax();
                    $("#tapTypeAdd").val("");
                }else if(data.returnCode==999994){

                    setUserTimeout(data);

                }else{
                    layer.msg(data.returnMessage,2,-1);
                }
            },
			error: function (message) {
				layer.msg(message,2,-1);
			}
		});	
	},
	//编辑标签类别
	"editTapTypeFrom":function(){
		var tapTypeCode = $("#tapcode").val();
		var tapType = $("#tapTypeEdit").val().trim();
//		var sort = $("#sortEext").val();
//		var oldSort = $("#oldSortEext").val();
//		var scrapped = $("#scrapped").val();
//		if(sort==""||sort==null) sort = -1;
		var jsons = {
			"tag_type":tapType,
			"tag_id":tapTypeCode,
//			"sort":parseInt(sort),
//			"oldSort":oldSort,
//			"scrapped":scrapped
		};
		return jsons;
	},
	//修改接口
	"postTapTypeEditAjax":function(tapTypeCode){
		$.ajax({
//			type: 'POST',
//			url: basePath + 'dictionary/updateDictionary',
			//模拟接口数据
			type: 'POST',
			url: basePath + 'Tagtype/modifyTagtype.do',
			data: savaData.editTapTypeFrom(),
			success: function (data) {
                if (data.returnCode==10000) {
                    layer.msg(data.returnMessage,1,10);   
                    layer.close(pageTapTypeEdit);
                    q.publicAjax(curPage,numList,"");     
					s.tapTypePublicAjax();
                }else if(data.returnCode==999994){

                    setUserTimeout(data);

                }else{
                    layer.msg(data.returnMessage,2,-1);
                }
            },
			error: function (message) {
				layer.msg(message,2,-1);
			}
		});	
	},
	
	
	
		//编辑获取标签值
	"getTapValueFormVal":function(data){
		var tapValue =data.returnData.tapValue;
		$("#tapValueEdit").val(tapValue);
	},
	//public loadAjax
//	"loadTapValueAjax":function(tapValueCode){
//		$.ajax({
////			type: "POST",
////	        url: basePath+"dictionary/selectByPrimaryKey",
//			//模拟接口数据
//			type: "GET",
//	        url: basePath+"json/dictionary/selectByPrimaryKey.json",
//	        data: {"tapValueCode":tapValueCode},
//	        success: function (data) {
//                if (data.returnCode==0) {
//    
//                    if (data.returnCode==0&&data.returnData!=null&& data.returnData.length!=0) {
//                        savaData.getTapValueFormVal(data);
////                      $("#sortEext").attr('data-vi',data.returnData.maxSort)
//                    }else{
//                           layer.msg("加载失败，请重试！",2,-1);
//                    }
//                
//                } else if(data.returnCode==999994){
//
//                    setUserTimeout(data);
//
//                }},
//	        error: function (message) {
//	            layer.msg("加载失败，请重试！",2,-1);
//	        }
//	    });	
//	},
	
	
	
	//标签值数据处理
	"addTapValueFrom":function(){
		var TagvalueAdd = $("#TagvalueAdd").val().trim();
		var tapCode = $("#tapcode").val();
		var jsons = {
			"tag_value":TagvalueAdd,
			"tag_id":tapCode
		};
		return jsons;
	},
	//新增标签值
	"postTapValueAjax":function(){
		$.ajax({
//			type: 'POST',
//			url: basePath + 'dictionary/insertDictionary',
			//模拟接口数据
			type: 'POST',
			url: basePath + 'Tagvalue/addTagvalue.do',
			data: savaData.addTapValueFrom(),
			success: function (data) {
                if (data.returnCode==10000) {
                    layer.msg(data.returnMessage,1,10);   
                    layer.close(pageTapValue);
                    curPage=1;
                    q.publicAjax(curPage,numList,"");     
                    $("#TagvalueAdd").val("");
                }else if(data.returnCode==999994){

                    setUserTimeout(data);

                }else{
                    layer.msg(data.returnMessage,2,-1);
                }
            },
			error: function (message) {
				layer.msg(message,2,-1);
			}
		});	
	},
	//编辑标签值
	"editTapValueFrom":function(){
		var tapValueCode = $("#tapValueCode").val();
		var tapValue = $("#tapValueEdit").val();
		var tagvalue_id = $("#tapValueID").val();
//		var sort = $("#sortEext").val();
		var jsons = {
			"tag_value":tapValue,
			"tag_id":tapValueCode,
			"tagvalue_id":tagvalue_id,
//			"sort":parseInt(sort),
		};
		return jsons;
	},
	//修改接口
	"postTapValueEditAjax":function(tapValueCode){
		$.ajax({
//			type: 'POST',
//			url: basePath + 'dictionary/updateDictionary',
			//模拟接口数据
			type: 'POST',
			url: basePath + 'Tagvalue/modifyTagvalue.do',
			data: savaData.editTapValueFrom(),
			success: function (data) {
                if (data.returnCode==10000) {
                    layer.msg(data.returnMessage,1,10);   
                    layer.close(pageTapValueEdit);
                    curPage=1;
                   q.publicAjax(curPage,numList,"");     
                }else if(data.returnCode==999994){

                    setUserTimeout(data);

                }else{
                    layer.msg(data.returnMessage,2,-1);
                }
            },
			error: function (message) {
				layer.msg(message,2,-1);
			}
		});	
	}
}

//新增标签类别事件
$("#tapTypeAddArea").validationEngine({
	"focusFirstField": true,
	"maxErrorsPerField": "1"
});
$("#addTapTypeBtn").live("click",function(){
	var isSuccess = $('#tapTypeAddArea').validationEngine('validate');
	if(isSuccess==true){
	  	savaData.postTapTypeAjax();	
	}
});

//编辑标签类别事件
$("#tapTypeEditArea").validationEngine({
	"focusFirstField": true,
	"maxErrorsPerField": "1"
});
$("#editTapTypeBtn").live("click",function(){
	var isSuccess = $('#tapTypeEditArea').validationEngine('validate');
	if(isSuccess==true){
		savaData.postTapTypeEditAjax();	
	}
});

//新增标签值事件
$("#tapValueAddArea").validationEngine({
	"focusFirstField": true,
	"maxErrorsPerField": "1"
});
$("#addTapValueBtn").live('click', function(){
	var isSuccess = $('#tapValueAddArea').validationEngine('validate');
	if(isSuccess==true){
		savaData.postTapValueAjax();	
	}
});

//编辑标签值事件
$("#tapValueEditArea").validationEngine({
	"focusFirstField": true,
	"maxErrorsPerField": "1"
});
$("#editTapValueBtn").live('click',function(){
	var isSuccess = $('#tapValueEditArea').validationEngine('validate');
	if(isSuccess==true){
		savaData.postTapValueEditAjax();		
	}
})

//点击删除标签类别
$(".removeTypeBtn").live("click",function(){
//	var ids = parseInt($(this).attr("orgid"));
//	var ids = $("#tapcode").val();
	var ids = $(this).attr("tapTypeCode");
	$.layer({
		title:"温馨提示：",
	    shade: [0],
	    area: ['230','auto'],
	    dialog: {
	        msg: '确定删除吗？',
	        btns: 2,                    
	        type: -1,
	        btn: ['确定','取消'],
	        yes: function(){
	           remove.removeAjax(ids);
	        }, no: function(){
	            
	        }
	    }
	});
}); 

var remove ={
	//public removeAjax
	"removeAjax":function(ids){
		$.ajax({
//	        type: "POST",
//	        url: basePath + 'dictionary/deleteDictionary?dicValueCode='+dicvaluecode,
	        //模拟接口数据
	        type: "POST",
	        url: basePath + 'Tagtype/deleteTagtype.do?tag_id='+ids,	
	        success: function (data) {
                if (data.returnCode==10000) {
                    layer.msg(data.returnMessage,1,10);
                    s.tapTypePublicAjax();
//                  var listNum = $("#organizationList").find("li").length;
                    //列表当前页只有一条数据在删除后，并且还有上一页时，自动跳转到上一页
//                  var listNum = $(".ktab").find(".td").length;
//                  if(countPage==curPage&&listNum==1) curPage -=1;
                    q.publicAjax(curPage,numList,"");
                    window.location.reload();
                }else if(data.returnCode==999994){

                    setUserTimeout(data);

                }else{
                    layer.msg(data.returnMessage,2,-1);
                }
            },
	        error: function (message) {
	            layer.msg("删除数据失败！<br>"+message,2,-1);
	        }
	    });				      
	}
}
//页面初始化加载数据
//q.publicAjax(curPage,numList,"");	

//点击删除标签值
$(".removeValueBtn").live("click",function(){
//	var ids = parseInt($(this).attr("orgid"));
	var tagvalue_id = $(this).attr("tagvalue_id");
	$.layer({
		title:"温馨提示：",
	    shade: [0],
	    area: ['230','auto'],
	    dialog: {
	        msg: '确定删除吗？',
	        btns: 2,                    
	        type: -1,
	        btn: ['确定','取消'],
	        yes: function(){
	           removes.removeAjax(tagvalue_id);
	        }, no: function(){
	            
	        }
	    }
	});
}); 

var removes ={
	//public removeAjax
	"removeAjax":function(ids){
		$.ajax({
	        //模拟接口数据
	        type: "POST",
	        url: basePath + 'Tagvalue/deleteTagvalue.do?tagvalue_id='+ids,	
	        success: function (data) {
                if (data.returnCode==10000) {
                    layer.msg(data.returnMessage,1,10);
                    //列表当前页只有一条数据在删除后，并且还有上一页时，自动跳转到上一页
                    var listNum = $("#selectDicList").find(".td").length;
                    if(countPage==curPage&&listNum==1) curPage -=1;
                    q.publicAjax(curPage,numList,selectJsonData);
                    
                }else if(data.returnCode==999994){
                    setUserTimeout(data);
                }else{
                    layer.msg(data.returnMessage,2,-1);
                }
            },
	        error: function (message) {
	            layer.msg("删除数据失败！<br>"+message,2,-1);
	        }
	    });				      
	}
}

//页面初始化加载数据
//q.publicAjax(1,numList,"");